#include "interface.h"
#include "user_config.h"
#include "framework_ndt_common.h"
#include "sofrware_param.h"
#include "ndt_algorithm.h"
#include "pmu.h"

#include "calibration.h"
#include "hal_afe.h"
#include "hal_gpio.h"
#include "hal_iic.h"
#include "hal_flash.h"
#include "hal_temperature.h"
#include "hal_wdt.h"
#include "CSU18M68.h"



/**
 * @brief 通讯检查并唤醒
 * 
 */
static void IIC_Comm_Check(void)
{
	if(gPmuWorkMode != PMU_SHUT_DOWN_MODE)
	{
		if(gPmuIntoSleepDisable)
		{
			PMU_SetWorkMode(PMU_ACTIVE_MODE);				
		}	
	}	

}

char cal_check_sum_byte(unsigned char *data,unsigned char len)
{
	unsigned char sum = 0;
	while(len>0)
	{
		len--;
		sum += *data++;
	}
	return sum;
}
/**
 * @brief 计算指定指针内数据之和
 * 
 * @param data 指针
 * @param len 需要计算的长度
 * @return signed short int 
 */
signed short int cal_check_sum(unsigned char *data, unsigned char len)
{
//    unsigned char i;
//    signed int sum = 0;
//    for(i = 0; i < len; i++){
//        sum += data[i];
//    }
//    return (0-sum);

    unsigned char i;
    signed short int sum = 0;
    
	while(len>0)
    {
    	sum += *data++;
		sum += (unsigned int)(*data++) << 8;
		
		len -= 2;
    
    }
    
    return (0-sum);
    
}
/**
 * @brief debug数据指针赋值
 * 
 */
void debug_force_value(void)
{
	int *iicPorint;
	char i;
	iicPorint = (signed int *)&Reg_DebugDataB[0];
	if(Reg_DataReadyB[0] == 0)
	{	
	
		for(i = 0 ;i<CHANNEL_NUM;i++)
		{
			*iicPorint++ = Get_AdcData_offset(i);
		}
		
		for(i = 0 ;i<CHANNEL_NUM;i++)
		{
			*iicPorint++ = Get_Rawdata(i);
		}
		for(i = 0 ;i<CHANNEL_NUM;i++)
		{
			*iicPorint++ = Get_Base_Line(i);
		}
		
		for(i = 0 ;i<CHANNEL_NUM;i++)
		{
			*iicPorint++ = Get_ForceData(i);
		}
		for(i = 0 ;i<CHANNEL_NUM;i++)
		{
			*iicPorint++ = Get_Forceflag(i);
		}
		

		Reg_DataReadyB[0] = 10;	
	}
}
/**
 * @brief 校准数据指针赋值
 * 
 */
void debug_cal_process_value(void)
{
	int *iicPorint;
	int temp;
	unsigned char count = 0;
	iicPorint = (signed int *)&Reg_DebugDataB[0];
	if(Reg_DataReadyB[0] == 0)
	{	
		*iicPorint++ = Get_AdcData_offset(0);
		*iicPorint++ = gCalProcessState;
		*iicPorint++ = gCalStatuOutput;
		*iicPorint++ = gForceFactor[0];
//		
		//*iicPorint++ = cal_check_sum((unsigned char*)Reg_DebugDataB,16);
		
		Reg_DataReadyB[0] = 8;	
	}
}
/**
 * @brief 动态数据接口
 * 
 */
static void DebugMode_Process(void)
{	
	
	
	switch(Reg_DebugModeB[0])
	{		
		
		case REG_MODE_DEBUG_DATA:					
			debug_force_value();
		break;

		case REG_MODE_CAL_DATA:							
			debug_cal_process_value();
		break;
	
		default:
			Reg_DebugModeB[0] = 0;
			break;
	}

}
/**
 * @brief 按压阈值更新
 * 
 */
void Update_Button_Force_Tri()
{

	int temp_button_tri[CHANNEL_NUM];
	char temp;
	char flag;
	char i;
//	//判断是否校验成功
//	if(cal_check_sum_byte((unsigned char *)Reg_ButtonForceTri,INFOR_DATA_LEN-1) != Reg_ButtonForceTri[INFOR_DATA_LEN-1])
//	{
//		return ;
//	}
	
	for(i = 0;i<CHANNEL_NUM;i++)
	{
		temp_button_tri[i] = Reg_ButtonForceTri[2*i];
		temp_button_tri[i] <<= 8;
		temp_button_tri[i] |= Reg_ButtonForceTri[2*i+1];
		temp = EEPROM_DATA_ADDR_BUTTON0_LEVEL+3*i;
		flag = EEPROM_StorageInt(temp, temp_button_tri[CH0]);	
		if(flag == 0) 
		{
			gKeyDownLevel[CH0] = EEPROM_ReadInt(temp);
		}
	}

}
/**
 * @brief 校准系数更新
 * 
 * @param calBuf 校准系数缓存指针
 */

void Update_Cal_Factor()
{
	int temp_factor[CHANNEL_NUM];
	char flag,temp;;
	char i;
	for(i = 0;i<CHANNEL_NUM;i++)
	{
		temp_factor[i] = Reg_RawDataFactor[2*i];
		temp_factor[i] <<= 8;
		temp_factor[i] |= Reg_RawDataFactor[2*i+1];
		temp = EEPROM_DATA_ADDR_CH0FACTOR+3*i;
		flag = EEPROM_StorageInt(temp, temp_factor[CH0]);	
		if(flag == 0) 
		{
			gForceFactor[CH0] = EEPROM_ReadInt(temp);
		}
	}
}
/**
 * @brief IIC握手接口
 * 
 */
void Handshake_detect()
{ 
	int temp;
	temp = Reg_Handshake[0]+ Reg_Handshake[1];    //校验，相加为0X100
	if((temp&0xff) == 0)//
	{
		switch(Reg_Handshake[0])
		{
			case REG_BUTTON_FORCE_TRI:  //更新阈值 0x5b 0xa5
					Update_Button_Force_Tri();
				break;
			case REG_CAL_START_CMD:    //开始校准  0x12 0xee
					Calibration_Proces_data_init();	
					gCalibration_flag = 1;	
				break;
			case REG_CAL_END_CMD:	 //结束校准   0x13 0xed
					gCalibration_flag = 0;	
				break;
			case REG_SYS_RESET_CMD:	 //系统软复位  0x02  0xfe
					Sys_Reset();  //0x01 0xcc
			break;
			
			case REG_SET_CAL_FACTOR_CMD:   // 更新校准系数 0x24 0xdc
				Update_Cal_Factor();
			break;
			
			case REG_SYS_WAKEUP_CMD:    //唤醒 0x05   0xfb
			
				break;
				
			case REG_SYS_SLEEP_CMD:     //休眠 0x06   0xfc
			
				break;
		
			default:
				break;
		}
		

		Reg_Handshake[0] = 0x00;
		Reg_Handshake[1] = 0x00;
	}


}

/**
 * @brief 静态寄存器更新
 * 
 */
static void Reg_Monitor(void)
{
 	char i;
	//固件版本号 0x10
	Reg_FWVersion[0] = FW_ITEM_H; 
	Reg_FWVersion[1] = FW_ITEM_L;
	Reg_FWVersion[2] = FW_VER_H;
	Reg_FWVersion[3] = FW_VER_L;

	//force 0x21
	for(i = 0;i<CHANNEL_NUM;i++)
 	{
		Reg_ForceData[2*i] = Get_ForceData(i);
		Reg_ForceData[2*i+1] = Get_ForceData(i)>>8;	
 	}
	Reg_ForceData[INFOR_DATA_LEN-1] =cal_check_sum_byte((unsigned char *)Reg_ForceData,INFOR_DATA_LEN-1);
	//offset 0x17
	for(i = 0;i<CHANNEL_NUM;i++)
 	{
		Reg_OffsetData[2*i] = gOffsetData[i];
		Reg_OffsetData[2*i+1] = gOffsetData[i]>>8;	
 	}
	Reg_OffsetData[INFOR_DATA_LEN-1] =cal_check_sum_byte((unsigned char *)Reg_OffsetData,INFOR_DATA_LEN-1);
//	
	//校准系数 0x24 
	for(i = 0;i<CHANNEL_NUM;i++)
 	{
		Reg_CalFactorData[2*i] = gForceFactor[i];
		Reg_CalFactorData[2*i+1] = gForceFactor[i]>>8;	
 	}
 	Reg_CalFactorData[INFOR_DATA_LEN-1] =cal_check_sum_byte((unsigned char *)Reg_CalFactorData,INFOR_DATA_LEN-1);
// 	
// 	
	//按键阈值
	for(i = 0;i<CHANNEL_NUM;i++)
 	{
		Reg_ButtonForceTri[2*i] = gKeyDownLevel[i];
		Reg_ButtonForceTri[2*i+1] = gKeyDownLevel[i]>>8;	
 	}
 	Reg_ButtonForceTri[INFOR_DATA_LEN-1] =cal_check_sum_byte((unsigned char *)Reg_ButtonForceTri,INFOR_DATA_LEN-1);
 	
 	//原始ADC数据 0x22
	for(i = 0;i<CHANNEL_NUM;i++)
 	{
		Reg_AdcData[2*i] = Get_AdcData_offset(i);
		Reg_AdcData[2*i+1] = Get_AdcData_offset(i)>>8;	
 	}
 	Reg_AdcData[INFOR_DATA_LEN-1] =cal_check_sum_byte((unsigned char *)Reg_AdcData,INFOR_DATA_LEN-1);
 	
 	
 	
 	//校准状态 0x14 
	Reg_CalStatus[0] = Get_Cal_Step();
	Reg_CalStatus[1] = Get_Cal_State();
	Reg_CalStatus[2] = 	Reg_CalStatus[0] + Reg_CalStatus[1];
 	

}

/**
 * @brief IIC通讯过程
 * 
 */
void Interface_Proc(void)
{

	IIC_Comm_Check();		// IIC检查
	
	DebugMode_Process();	//动态接口

	Reg_Monitor();			//静态接口
	
	Handshake_detect();     //握手接口
   
}


